import { MotaInterface } from '@/mota';
import { MapInfo } from '@/mota/data/MapDataUtils';
import ScrollTool from '@/mota/tools/ScrollTool';
import BuildUtils from '@/mota/utils/BuildUtils';

export default class BuildFloor extends ScrollTool<MapInfo> implements MotaInterface {

	public build: BuildUtils;

	public ZIndex: number = 5;

	public constructor(build: BuildUtils) {
		super({
			Left: 0,
			Top: 0.8,
			Width: 4,
			Height: 11 - 0.8,
			Padding: 0.1,
			ItemWidth: 4 - 0.2 - 0.1,
			ItemHeight: 0.8,
			ItemPadding: 0.1,
			ScrollWidth: 0.1,
		});
		this.build = build;
	}

	public CalcSize(): void {
		const { build: { mapList } } = this;
		this.List.Value = Array.from(mapList);
	}

	public InView(): boolean {
		const { build } = this;
		const { Mota } = build;
		const { onBuild } = Mota;
		return onBuild;
	}

	public JudgeKey: MotaInterface["JudgeKey"] = (keyUtils): void => {
		if (!this.InView()) { return; }

		const { build, SelectIndex, SelectItem } = this;

		for (const key of keyUtils.keyHistory) {
			if (key === "Escape" && SelectIndex !== undefined) {
				this.SelectIndex = undefined;
				keyUtils.delete(key);
			}
			else if (key === "ArrowUp" && keyUtils.has("Control")) {
				if (SelectIndex !== undefined) {
					const targetIndex = SelectIndex - 1;
					if (this.Swap(SelectIndex, targetIndex)) {
						build.mapList = this.List.Value;
						this.SelectIndex = targetIndex;
						this.CalcSize();
						this.EnsureVisible();
						console.log(this.List.Value);
						keyUtils.delete(key);
					}
				}
			}
			else if (key === "ArrowDown" && keyUtils.has("Control")) {
				if (SelectIndex !== undefined) {
					const targetIndex = SelectIndex + 1;
					if (this.Swap(SelectIndex, targetIndex)) {
						build.mapList = this.List.Value;
						this.SelectIndex = targetIndex;
						this.CalcSize();
						this.EnsureVisible();
						keyUtils.delete(key);
					}
				}
			}
			else if (key === "ArrowUp") {
				this.SelectLast(true);
				keyUtils.delete(key);
			}
			else if (key === "ArrowDown") {
				this.SelectNext(true);
				keyUtils.delete(key);
			} else if ((key === "Enter" || key === " ")) {
				if (SelectItem !== undefined) {
					// build.select.Open(SelectItem).then(() => {
					// 	build.buildState = "none";
					// });
					// build.buildState = "select";
					keyUtils.delete(key);
				}
			}
			else if ((key === "Backspace" || key === "Delete")) {
				if (SelectItem !== undefined) {
					// build.DeleteUnit(SelectItem)
					keyUtils.delete(key);
				}
			}
			else if ((key === "a")) {
				build.append.Open().then(() => {
					build.buildState = "none";
				});
				build.buildState = "append";
				keyUtils.delete(key);
			}
		}
	}

	public Draw: MotaInterface["Draw"] = (draw) => {
		if (!this.InView()) { return; }

		const { _2d } = draw;
		const { Left: { Value: left }, Top: { Value: top } } = this;

		_2d.save();
		_2d.fillStyle = "#FFFFFF";
		_2d.strokeStyle = "#00FF00";
		draw.SetFont(400, 1 / 3, "Mota");
		_2d.textAlign = "left";
		_2d.textBaseline = "top";
		draw.SetLineWidth(1 / 16);

		draw.fillText("新增地图", left + 1.35, top - 0.65);

		_2d.save();
		for (const [mapInfo, isSelect, [oX, oY, ,], [x, y, w, h]] of this.GetItemsSize()) {
			const { floor, name } = mapInfo;
			draw.fillText(`${floor}`, oX, oY + 0.1);
			draw.fillText(`${name}`, oX + 0.9, oY + 0.1);
			if (isSelect) {
				draw.strokeRect(x, y, w, h);
			}
		}
		_2d.restore();

		{
			const scrollSize = this.GetScrollSize();
			if (scrollSize) {
				const [[x, y, w, h], [sx, sy, sw, sh]] = scrollSize;
				_2d.fillStyle = "#00000044";
				draw.fillRect(x - 0.05, y - 0.05, w, h);
				_2d.fillStyle = "#000";
				draw.fillRect(sx - 0.05, sy - 0.05, sw, sh);
			}
		}

		_2d.restore();
	}
}
